package com.zmm.weblog.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmm.weblog.common.domain.dos.*;
import com.zmm.weblog.common.domain.mapper.*;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.exception.BizException;
import com.zmm.weblog.common.utils.PageResponse;
import com.zmm.weblog.common.utils.Response;
import com.zmm.weblog.web.convert.ArticleConvert;
import com.zmm.weblog.web.convert.CategoryConvert;
import com.zmm.weblog.web.module.vo.category.FindCategoryArticlePageListReqVo;
import com.zmm.weblog.web.module.vo.category.FindCategoryArticlePageListRspVo;
import com.zmm.weblog.web.module.vo.category.FindCategoryListRspVo;
import com.zmm.weblog.web.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: 福禄寿
 * @Date: 2024/7/8 下午4:55
 * @Des: weblog-springboot
 **/
@Service
public class CategoryServiceImpl implements CategoryService {


    private static final Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Override
    public Response findCategoryList() {
        //获取分类信息
        List<CategoryDo> categoryDos = categoryMapper.selectList(null);
        List<FindCategoryListRspVo> vos = null;
        if (!CollectionUtils.isEmpty(categoryDos)){
            //DO转VO
            vos = categoryDos.stream()
                    .map(CategoryConvert.INSTANCE::convertDO2VO).collect(Collectors.toList());
        }
        //获取标签信息
        return Response.success(vos);
    }

    @Override
    public Response findCategoryArticlePageList(FindCategoryArticlePageListReqVo findCategoryArticlePageListReqVo) {
        Long current = findCategoryArticlePageListReqVo.getCurrent();
        Long size = findCategoryArticlePageListReqVo.getSize();
        Long categoryId = findCategoryArticlePageListReqVo.getId();

        CategoryDo categoryDo = categoryMapper.selectById(categoryId);
        if (Objects.isNull(categoryDo)){
            log.info("==>该分类不存在，categoryId:{}",categoryId);
            throw  new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
        }

        List<ArticleCategoryRelDo> articleCategoryRelDos = articleCategoryRelMapper.selectListByCategoryId(categoryId);
        //若该分类下未发布任何文章
        if (CollectionUtils.isEmpty(articleCategoryRelDos)){
            log.info("==>该分类下未发布任何文章，categoryId:{}",categoryId);
            return PageResponse.success(null,null);
        }

        List<Long> articleIds = articleCategoryRelDos.stream().map(ArticleCategoryRelDo::getArticleId).collect(Collectors.toList());
        Page<ArticleDo> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDo> articleDos = page.getRecords();
        List<FindCategoryArticlePageListRspVo> vos = null;
        if (!CollectionUtils.isEmpty(articleDos)){
            //DO转VO
             vos = articleDos.stream()
                    .map(ArticleConvert.INSTANCE::convertDO2CategoryArticleVO).collect(Collectors.toList());
        }
        return PageResponse.success(page,vos);
    }
}
